package com.higgins.lawapp.client.maintenance.components;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.higgins.lawapp.client.maintenance.model.DataItemModel;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemLevelDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemSourceDTO;

public class DataItemLevelListBox extends LayoutPanel implements ChangeHandler 	{
	private DataItemSourceDTO dataItemSourceSelectedDTO;
	private DataItemModel dataItemModel;
	private DataItemInstanceListBox dataItemInstanceListBox;
	private DataSourceLevelDeleteButton deleteButton;
	private ListBox listBox = new ListBox();
	private DataItemTable dataItemTable;
//	private List<DataItemLevelDTO> dataItemLevelDTOs;
	
	public DataItemLevelListBox(DataItemModel dataItemModel, DataItemInstanceListBox dataItemInstanceListBox, DataItemTable dataItemTable)	{
		this.dataItemModel = dataItemModel;
		this.dataItemInstanceListBox = dataItemInstanceListBox;
		this.dataItemTable = dataItemTable;
		setWidth("800px");
		setHeight("25px");

		deleteButton = new DataSourceLevelDeleteButton(dataItemModel);
		listBox.addItem("None");
		listBox.setSelectedIndex(0);
		listBox.addChangeHandler(this);
		deleteButton.setTitle("Delete Level");
		deleteButton.setText("Delete Level");
		add(listBox);
		add(deleteButton);
	    setWidgetLeftWidth(listBox, 0, Unit.PCT, 80, Unit.PCT);
	    setWidgetRightWidth(deleteButton, 0, Unit.PCT, 20, Unit.PCT);
	}
	
	public void setDataItemSourceSeleced(DataItemSourceDTO dataItemSourceSelectedDTO)	{
		this.dataItemSourceSelectedDTO = dataItemSourceSelectedDTO;

		clearList();
		addDataItemSourceLevelNamesToList(dataItemSourceSelectedDTO);
		onChange(null);
	}
	
	@Override
	public void onChange(ChangeEvent event) {
		dataItemModel.setDataItemLevelSelected(getDataItemLevelDTOSelected());
		dataItemInstanceListBox.setDataItemLevelDTOSelected(getDataItemLevelDTOSelected());
	}

	public DataItemLevelDTO getDataItemLevelDTOSelected()	{
		DataItemLevelDTO dataItemLevelDTO = null;
		if (listBox.getSelectedIndex() > 0)	{
			int selectedIndex = listBox.getSelectedIndex() - 1;
			if (dataItemSourceSelectedDTO!=null)	{
				List<DataItemLevelDTO> dataItemLevelDTOs = dataItemSourceSelectedDTO.getDataItemLevelDTOs();
				dataItemLevelDTO = dataItemLevelDTOs.get(selectedIndex);
			}
		}
		return dataItemLevelDTO ;
	}

	public void addItem(String item)	{
		listBox.addItem(item);
	}
	public void setSelectedIndex(int index)	{
		listBox.setSelectedIndex(index);
	}
	public void addDataItemSourceLevelNamesToList(DataItemSourceDTO dataItemSourceSelectedDTO)	{
		if (dataItemSourceSelectedDTO!=null)		{
			List<DataItemLevelDTO> dataItemLevelDTOs = dataItemSourceSelectedDTO.getDataItemLevelDTOs();
			if (dataItemLevelDTOs!=null)	{
				Iterator<DataItemLevelDTO> dataItemLevelDTOsIterator = dataItemLevelDTOs.iterator();
				while (dataItemLevelDTOsIterator.hasNext())	{
					DataItemLevelDTO dataItemLevelDTO = dataItemLevelDTOsIterator.next();
					listBox.addItem(dataItemLevelDTO.getDataItemLevelName());		}
			}
		}
	}
	public void clearList()	{
		listBox.clear();
		listBox.addItem("None");
		listBox.setSelectedIndex(0);
	}
}
